package com.mengwen.backstage.service.impl;

import com.mengwen.backstage.mapper.GetNotificationMapper;
import com.mengwen.backstage.mapper.InvitationMapper;
import com.mengwen.backstage.mapper.NotificationMapper;
import com.mengwen.backstage.pojo.GetNotification;
import com.mengwen.backstage.pojo.Notification;
import com.mengwen.backstage.service.NotificationService;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.UUID;

/**
 * @author LiangYonghui
 * @date 2020/4/8 15:33
 * @description 通知的业务层实现类
 */
@Service
public class NotificationServiceImpl implements NotificationService {
    private static final Log LOG = LogFactory.getLog(NotificationServiceImpl.class);

    @Autowired
    NotificationMapper notificationMapper;
    @Autowired
    GetNotificationMapper getNotificationMapper;
    @Autowired
    InvitationMapper invitationMapper;
    @Override
    public int addNotification(Notification notification) {

        // 获取发送者的unionid
        String inviterUnionid = notification.getUnionid();
        // 用notification里的unionid
        // 结合邀请表查询出自己旗下的被邀请人的所有unionid(inviteeUnionid)
        List<String> inviteeUnionids = invitationMapper.findInvitee(inviterUnionid);
        // 根据unionid批量将通知内容放到获取通知的表
        GetNotification getNotification = new GetNotification();

        for (int i = 0; i < inviteeUnionids.size(); i++) {
            getNotification.setContent(notification.getContent());
            getNotification.setNoticeDate(notification.getNoticeDate());
            getNotification.setSenderNickname(notification.getNickname());
            getNotification.setId(UUID.randomUUID().toString());
            getNotification.setGetterUnionid(inviteeUnionids.get(i));
            int count = getNotificationMapper.insertItemByGetNotification(getNotification);
            if (count == -1) {
                LOG.error("插入通知失败：" + notification.getUnionid());
            }
        }


        return  notificationMapper.addNotification(notification);
    }
}
